from django.shortcuts import render
from django.views import View
from rest_framework import status
from rest_framework.decorators import action
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response

from rest_framework.viewsets import ReadOnlyModelViewSet, ModelViewSet

from community.models import Label, Article,Comment
from community.serializers import LabelSerializer, ArticleSerializer, CommentSerializer, SonCommentSerializer
from .paginations import ArticleLimitOffsetPagination,ArticlePageNumberPagination
from django.db.models import F
# Create your views here.

# 标签不需要增删改的接口
# 只需要查询接口


class LabelViewSet(ReadOnlyModelViewSet):
    # 查询操作
    queryset = Label.objects.all()
    serializer_class = LabelSerializer
    # 标签只有登录才能看到
    permission_classes = [IsAuthenticated]

# 文章的增删改查
class ArticleViewSet(ModelViewSet):
    # queryset = Article.objects.filter(is_delete=False).filter(status = 1)
    queryset = Article.objects.filter(is_delete=False)
    serializer_class = ArticleSerializer
    permission_classes = [IsAuthenticated]
    # 分页器
    pagination_class = ArticleLimitOffsetPagination
    # pagination_class = ArticlePageNumberPagination

    # 重新查询集
    def get_queryset(self):
        # 查看文章  发布的文章才能查看
        if self.action in ["list", "retrieve"]:
            return self.queryset.filter(status=1)
        else:
            return self.queryset

    # 重写序列器
    def get_serializer_class(self):
        if self.action == "comment":
            return CommentSerializer
        else:
            return ArticleSerializer

    # # create方法重写一下
    def create(self, request, *args, **kwargs):
        request.POST._mutable = True # 允许修改post传入的参数  1     2
        request.data["user"] = request.user.id
        # 因为重写create方法 还需要用到 create的功能
        return ModelViewSet.create(self, request, *args, **kwargs)


    # 浏览量
    def retrieve(self, request, *args, **kwargs):
        # 文章id？？  kwargs["pk"]
        # 修改操作
        self.get_queryset().filter(id = kwargs["pk"]).update(page_view = F("page_view")+1)
        return ModelViewSet.retrieve(self, request, *args, **kwargs)

    # 获取当前文章的所有一级评论  detail=True 要传id
    @action(methods=['get'], detail=True)
    def comment(self,request, pk):
        try:
            article = self.get_queryset().get(id=pk)
        except Article.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        # 根据文章 进行关联查询  得到一级评论
        comments = article.comment_set.filter(parent_comment__isnull=True)
        # 添加分页器
        comments = self.paginate_queryset(comments)
        # # 序列化 评论的序列化
        serializer = self.get_serializer(comments,many=True)
        # # 返回
        # return Response(serializer.data)
        return self.get_paginated_response(serializer.data)




class CommentViewSet(ModelViewSet):
    queryset = Comment.objects.filter(is_delete=False,article__is_delete=False,article__status=1)
    serializer_class = CommentSerializer
    permission_classes = [IsAuthenticated]
    pagination_class = ArticleLimitOffsetPagination

    def get_serializer_class(self):
        if self.action == "retrieve":
            return SonCommentSerializer
        else:
            return CommentSerializer

    def create(self, request, *args, **kwargs):
        request.POST._mutable = True  # 允许修改post传入的参数  1     2
        request.data["user"] = request.user.id
        # 因为重写create方法 还需要用到 create的功能
        return ModelViewSet.create(self, request, *args, **kwargs)

    # 展示当前一级评论下面的子级评论
    def retrieve(self, request, *args, **kwargs):
        try:
            comment = self.get_queryset().get(id = kwargs["pk"])
        except Comment.DoesNotExist:
            return Response(status=status.HTTP_404_NOT_FOUND)
        # 有一级评论 反向查询 得到子级评论
        comments = comment.parent_comment_set.all()
        # 添加分页器
        comments = self.paginate_queryset(comments)
        serializer = self.get_serializer(comments,many=True)
        # 返回
        # return Response(serializer.data)
        return self.get_paginated_response(serializer.data)
